package com.android.jblm.commandcentral;

import android.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.jblm.commandcentral.objects.Contact;

/**
 * Created by Brian on 8/21/2014.
 */
public class ContactFragment extends Fragment {

    private Contact myContact;
    private getContactListener mCallBack;

    public interface getContactListener{
        Contact getCurContact();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        myContact = mCallBack.getCurContact();

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.contact_view, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();

        updateView();

    }

    public void updateView() {

        final android.app.FragmentManager fm = getFragmentManager();
        TextView name = (TextView) getActivity().findViewById(R.id.contactName);
        TextView desc = (TextView) getActivity().findViewById(R.id.contactDesc);

        name.setText(myContact.contactName);
        desc.setText(myContact.contactDesc);

        Button phones = (Button) getActivity().findViewById(R.id.callButton);
        Button web = (Button) getActivity().findViewById(R.id.webButton);
        Button email = (Button) getActivity().findViewById(R.id.emailButton);

        phones.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Create fragment and give it an argument for the selected article
                PhoneListFragment newFragment = new PhoneListFragment();

                getChildFragmentManager().beginTransaction().replace(R.id.fragment_container, newFragment).addToBackStack(null).commit();

                android.app.FragmentTransaction transaction = fm.beginTransaction();

                // Replace whatever is in the fragment_container view with this fragment,
                // and add the transaction to the back stack so the user can navigate back
                transaction.replace(R.id.fragment_container, newFragment);
                transaction.addToBackStack(null);

                // Commit the transaction
                transaction.commit();
            }
        });

        web.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = myContact.contactURL;

                Uri link = Uri.parse(url);
                Intent launchBrowser = new Intent(Intent.ACTION_VIEW, link);
                v.getContext().startActivity(launchBrowser);
            }
        });

        email.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                        "mailto",myContact.contactEmail, null));
                v.getContext().startActivity(Intent.createChooser(intent, "Choose an Email client :"));

            }
        });



    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }

}
